const {invalidGoodsId, cartFormatError} = require('../constant/error.type');
const { goodsIsExists } = require('../service/cart.service');
const validator = (rules) => {
    return async (ctx, next) => {
        try {
            ctx.verifyParams(rules)
        } catch (err) {
            console.error(err);
            invalidGoodsId.result = err
            return ctx.app.emit('error', cartFormatError, ctx)
        }
        await next()
    }
}
const goodsIdValidator = async (ctx, next) => {
    if (!await goodsIsExists(ctx.request.body.goods_id)) {
        return ctx.app.emit('error', invalidGoodsId, ctx)
    }
    await next()
}
module.exports =  {
    validator,
    goodsIdValidator
}